package com.ship.boats.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ship.boats.domain.SpBoastPortMessage;
import com.ship.boats.domain.dto.QueryBoatsPortRq;
import com.ship.boats.domain.vo.QueryBoastPortMessageInfo;
import com.ship.boats.mapper.SpBoastPortMessageMapper;
import com.ship.boats.service.SpBoastPortMessageService;
import com.ship.common.core.utils.bean.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <b>SpBoastPortMessageServiceImpl</b>
 *
 * @description: SpBoastPortMessageServiceImpl <br>
 * @date: 2024/4/3 11:05 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class SpBoastPortMessageServiceImpl extends ServiceImpl<SpBoastPortMessageMapper, SpBoastPortMessage> implements SpBoastPortMessageService {
    private static final Logger logger = LoggerFactory.getLogger(SpBoastPortMessageServiceImpl.class);

    @Override
    public PageInfo<QueryBoastPortMessageInfo> queryBoatsPortByImo(QueryBoatsPortRq rq) {
        PageHelper.startPage(rq.getPageNum(),rq.getPageSize());
        List<SpBoastPortMessage> boastPortMessageList = baseMapper.queryBoatsPortByImo(rq);
        PageInfo<SpBoastPortMessage> pageInfo = new PageInfo<>(boastPortMessageList);
        PageInfo<QueryBoastPortMessageInfo> queryBoastPortMessageInfoPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo,queryBoastPortMessageInfoPageInfo);
        List<QueryBoastPortMessageInfo> portMessageList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(boastPortMessageList)){
            boastPortMessageList.forEach(item -> {
                QueryBoastPortMessageInfo queryBoastPortMessageInfo = new QueryBoastPortMessageInfo();
                queryBoastPortMessageInfo.setId(item.getId());
                queryBoastPortMessageInfo.setShipName(item.getShipName());
                queryBoastPortMessageInfo.setImo(item.getImo());
                queryBoastPortMessageInfo.setShipId(item.getShipId());
                queryBoastPortMessageInfo.setNavStatus(item.getNavStatus());
                if (null != item.getInTime()){
                    queryBoastPortMessageInfo.setInTime(DateUtil.format(item.getInTime(),DatePattern.NORM_DATETIME_FORMATTER));
                }
                if (null != item.getOutTime()){
                    queryBoastPortMessageInfo.setOutTime(DateUtil.format(item.getOutTime(),DatePattern.NORM_DATETIME_FORMATTER));
                }
                queryBoastPortMessageInfo.setPortNameEn(item.getPortNameEn());
                queryBoastPortMessageInfo.setPortNameChs(item.getPortNameChs());
                queryBoastPortMessageInfo.setInPortDraught(new BigDecimal(item.getInPortDraught()).divide(new BigDecimal(10)));
                queryBoastPortMessageInfo.setOutPortDraught(new BigDecimal(item.getOutPortDraught()).divide(new BigDecimal(10)));
                BigDecimal time = new BigDecimal(item.getLayUpTime()).divide(new BigDecimal(86400),2, RoundingMode.HALF_UP);
                queryBoastPortMessageInfo.setLayUpTime(time.toString());
                queryBoastPortMessageInfo.setTimeZone(item.getTimeZone());
                queryBoastPortMessageInfo.setSisterStatus(item.getSisterStatus());
                queryBoastPortMessageInfo.setEnable(item.getEnable());
                queryBoastPortMessageInfo.setCreateTime(item.getCreateTime());
                queryBoastPortMessageInfo.setUpdateTime(item.getUpdateTime());
                portMessageList.add(queryBoastPortMessageInfo);
            });
        }
        queryBoastPortMessageInfoPageInfo.setList(portMessageList);
        return queryBoastPortMessageInfoPageInfo;
    }

    @Override
    public void renewalBoatsPortMessageByImo(String imo, List<SpBoastPortMessage> boatsPortMessageList) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH,-1);
        Date date = calendar.getTime();
        String startDate = DateUtil.format(date, DatePattern.NORM_DATE_FORMATTER);
        String endDate = DateUtil.format(new Date(), DatePattern.NORM_DATE_FORMATTER);
        //根据imo号删除所有船舶挂靠记录
        baseMapper.deleteBoatsPort(imo,startDate,endDate);
        logger.info("保存船舶挂靠港信息:{}"+ JSONUtil.parse(boatsPortMessageList));
        this.saveBatch(boatsPortMessageList);
    }
}
